//
//  UIColor+helper.m
//  ClassicPhoto
//
//  Created by RYAN on 15/5/11.
//  Copyright (c) 2015年 ryan. All rights reserved.
//

#import "UIColor+helper.h"

@implementation UIColor (helper)

+ (CGFloat)colorComponentFrom:(NSString *)string start:(NSUInteger)start length: (NSUInteger)length{
    
    NSString *substring = [string substringWithRange: NSMakeRange(start, length)];
    
    NSString *fullHex = length == 2 ? substring : [NSString stringWithFormat: @"%@%@", substring, substring];
    
    unsigned hexComponent;
    
    [[NSScanner scannerWithString: fullHex] scanHexInt: &hexComponent];
    
    return hexComponent / 255.0;
    
}

+ (UIColor *)colorWithHexString:(NSString *)hexString{
    
    NSString *colorString = [[hexString stringByReplacingOccurrencesOfString: @"#"withString: @""] uppercaseString];
    
    CGFloat alpha, red, blue, green;
    
    switch ([colorString length]) {
            
        case 3: // #RGB
            
            alpha = 1.0f;
            
            red   = [self colorComponentFrom: colorString start: 0 length: 1];
            
            green = [self colorComponentFrom: colorString start: 1 length: 1];
            
            blue  = [self colorComponentFrom: colorString start: 2 length: 1];
            
            break;
            
        case 4: // #ARGB
            
            alpha = [self colorComponentFrom: colorString start: 0 length: 1];
            
            red   = [self colorComponentFrom: colorString start: 1 length: 1];
            
            green = [self colorComponentFrom: colorString start: 2 length: 1];
            
            blue  = [self colorComponentFrom: colorString start: 3 length: 1];
            
            break;
            
        case 6: // #RRGGBB
            
            alpha = 1.0f;
            
            red   = [self colorComponentFrom: colorString start: 0 length: 2];
            
            green = [self colorComponentFrom: colorString start: 2 length: 2];
            
            blue  = [self colorComponentFrom: colorString start: 4 length: 2];
            
            break;
            
        case 8: // #AARRGGBB
            
            alpha = [self colorComponentFrom: colorString start: 0 length: 2];
            
            red   = [self colorComponentFrom: colorString start: 2 length: 2];
            
            green = [self colorComponentFrom: colorString start: 4 length: 2];
            
            blue  = [self colorComponentFrom: colorString start: 6 length: 2];
            
            break;
            
        default:
            alpha = 1;
            red = 0;
            green = 0;
            blue = 0;
            break;
            
    }
    
    return [UIColor colorWithRed: red green: green blue: blue alpha: alpha];
    
}

+ (UIColor *)colorWithRGBValue:(NSString *)value{
    UIColor *color;
    NSRange range = [value rangeOfString:@"(" options:NSCaseInsensitiveSearch];
    unsigned long start = range.length + range.location;
    range = [value rangeOfString:@","];
    NSString *redStr = [value substringWithRange:NSMakeRange(start, range.location - start)];
    
    start = range.location + 1;
    range = [value rangeOfString:@"," options:NSCaseInsensitiveSearch range:NSMakeRange(start, value.length - start)];
    NSString *greenStr = [value substringWithRange:NSMakeRange(start, range.location - start)];
    
    start = range.location + 1;
    range = [value rangeOfString:@")" options:NSCaseInsensitiveSearch range:NSMakeRange(start, value.length - start)];
    NSString *blueStr = [value substringWithRange:NSMakeRange(start, range.location - start)];
    
//    NSLog(@"%@",[NSString stringWithFormat:@"%lf,%lf,%lf",[redStr floatValue],[greenStr floatValue],[blueStr floatValue]]);
    
    color = [UIColor colorWithRed:[redStr floatValue]/255.0 green:[greenStr floatValue]/255.0 blue:[blueStr floatValue]/255.0 alpha:1.0];

    return color;
}

+ (UIColor *)colorWithRGBAValue:(NSString *)value{
    UIColor *color;
    NSRange range = [value rangeOfString:@"," options:NSBackwardsSearch];
    unsigned long end = range.location - 1;
    range = [value rangeOfString:@"("];
    unsigned long start  = range.location + 1;
    NSString *rgbString = [NSString stringWithFormat:@"rgb(%@)",[value substringWithRange:NSMakeRange(start, end - start + 1)]];
    color = [UIColor colorWithRGBValue:rgbString];
    
    start = end + 2;
    range = [value rangeOfString:@")" options:NSBackwardsSearch];
    end = range.location - 1;
    NSString *alphaStr = [value substringWithRange:NSMakeRange(start, end - start + 1)];
    color = [color colorWithAlphaComponent:[alphaStr floatValue]];
    
    NSLog(@"%@",[NSString stringWithFormat:@"%@,alpha:%@",rgbString,alphaStr]);
    return color;
}

@end
